import React from 'react';

export default class WorkList extends React.Component {
  constructor(props) {
    super(props)
  }

  pad(n) {
    return n < 10 ? "0"+n : n;
  }

  onWorkOrderDeleteClick(data) {
    if (confirm("确认要取消该报修吗?")) {
      this.props.onWorkOrderDeleteClick({
        id: data.id
      })
    }
  }

  renderList() {
    var self = this;
    var content = [];
    if (this.props.data) {
      this.props.data.forEach(function(data, index) {
        var status = "",
            gross_amount = parseFloat(data.gross_amount).toFixed(2),
            type = ""

        if (data.unit && data.unit.user_unit_binding && data.unit.user_unit_binding.length > 0) {
          type = data.unit.user_unit_binding[0].user_type
        }

        var className = "master"
        var signClass = "sign "

        switch (data.status) {
          case 'APPLYING':
            status = '未处理'
            gross_amount = '未处理'
            className = "applying"
            signClass += ' glyphicon glyphicon-wrench '
            break;
          case 'WORKING':
            status = '处理中'
            className = "working"
            signClass += ' glyphicon glyphicon-adjust '
            break;
          case 'COMPLETE':
            status = '待评价'
            signClass += ' glyphicon glyphicon-ok '
            break;
          case 'FINISH':
            status = '已完成'
            signClass += ' glyphicon glyphicon-ok '
            break;
          case 'CANCEL':
            status = '已取消'
            break;
          default:
            break;
        }


        var priority = ""
        if (data.priority == 4) {
          priority = "低"
        }else if (data.priority == 3) {
          priority = "中"
        }else if (data.priority == 2) {
          priority = "高"
        }else if (data.priority == 1) {
          priority = "紧急"
        }
        var created_at = new Date(data.created_at)
        var time = created_at.getFullYear() +"/" +self.pad(created_at.getMonth()+1) + "/"
                    + self.pad(created_at.getDate()) + " " + self.pad(created_at.getHours())
                    + ":"+self.pad(created_at.getMinutes())
        let printUrl = "../api/work/print?id="+data.id

        let total_time = ""
        let complete_date
        if (data.complete_date) {
          complete_date = new Date(data.complete_date)
        }
        else {
          complete_date = new Date()
        }
        let total = complete_date.getTime() - created_at.getTime()
        const oneDay = 1000*60*60*24,
              oneHour = 1000*60*60,
              oneMinute = 1000*60
        if (total > oneDay) {
          var day = parseInt((total)/oneDay)
          var hour = parseInt((total - day*oneDay)/oneHour)
          var minute = parseInt((total-day*oneDay-hour*oneHour)/oneMinute)
          total_time = day+"天"+ hour +"小时" + minute +"分"
        }else {
          var hour = parseInt(total/oneHour),
              minute = parseInt((total - hour*oneHour)/oneMinute)
          total_time = hour+"小时" + minute+"分"
        }

        content.push((
          <tr className={className}>
            <td>{data.unit.unit_desc}</td>
            <td>{data.owner}</td>
            <td>{data.mobile}</td>
            <td>{type}</td>
            <td>{time}</td>
            <td>{total_time}</td>
            <td>{priority}</td>
            <td>{status}<span className={signClass}></span></td>
            <td>{gross_amount}</td>
            <td>
              <a onClick={(event)=>self.props.onDetailClick(data)}>报修详情</a>
              {
                // <a className="edit"
                //   onClick={(event)=>self.props.onWorkOrderLineClick(data)}>添加维修项目</a>
              }
              <a className="edit"
                onClick={(event)=>self.props.onWorkOrderStatusClick(data)}>处理状态</a>
              {self.renderListBtns(data.status, printUrl, data)}
            </td>
          </tr>
        ))
      })
    }

    return content
  }

  renderListBtns(status, url, data) {
    var self = this
    if (status == 'APPLYING') {
      return (
        <a className="edit" onClick={(event)=>self.onWorkOrderDeleteClick(data)}>取消</a>
      )
    }
    else if (status == 'CANCEL') {
      return ""
    }
    else {
      return (
        <a className="edit" href={url} target="__blank">打印</a>
      )
    }
  }

  render() {
    return(
      <div className="work-list setting-list">
        <table className="table">
          <thead>
            <tr className="table-title">
              <th>户号</th>
              <th>联系人</th>
              <th>联系方式</th>
              <th className="user_type">用户类型</th>
              <th>报修时间</th>
              <th>总计</th>
              <th>优先级</th>
              <th>状态</th>
              <th>项目总费用</th>
              <th className="infoTh">详情</th>
            </tr>
          </thead>
          <tbody>
            {this.renderList()}
          </tbody>
        </table>
      </div>
    )
  }
}
